package fiscal.entities.tables;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;

import exceptions.PersistenceExceptionMessage;
import fiscal.dao.PersistencePisCofins;
import generic.interfaces.GenericPersistence;
import generic.interfaces.IDEntity;
import generic.interfaces.PersistInFile;

@SuppressWarnings("rawtypes")
@Entity
public class PisCofins implements IDEntity<String>, GenericPersistence<PersistencePisCofins, String, PisCofins>, PersistInFile, Comparable<PisCofins>{

	public static PersistencePisCofins persist = new PersistencePisCofins();
	
	@Id
	@Column(length=4)
	private String codigo;
	// E para entrada e S para saida
	private Character sentido;
	@Column(length=150)
	private String descricao;
	private Double aliquotaEntrada;
	private Double aliquotaSaida;
	
	
	public void setId(String codigo) {
		this.codigo = codigo;
	}
	
	public String getId() {
		return this.codigo;
	}
	public String getDescricao() {
		return descricao;
	}
	public Character getSentido() {
		return sentido;
	}
	public void setSentido(Character sentido) {
		this.sentido = sentido;
	}
	public void setDescricao(String descricao) {
		this.descricao = descricao;
	}
	public Double getAliquotaEntrada() {
		return aliquotaEntrada;
	}
	public void setAliquotaEntrada(Double aliquotaEntrada) {
		this.aliquotaEntrada = aliquotaEntrada;
	}
	public Double getAliquotaSaida() {
		return aliquotaSaida;
	}
	public void setAliquotaSaida(Double aliquotaSaida) {
		this.aliquotaSaida = aliquotaSaida;
	}

	
	public PersistencePisCofins DAOInstance() {
		if (persist == null) {
			persist = new PersistencePisCofins();	
		}
		return persist;
	}

	
	public PisCofins find(String id) throws PersistenceExceptionMessage {
		return DAOInstance().find(id);
	}

	
	public void persist() throws PersistenceExceptionMessage {
		DAOInstance().persist(this);
	}

	
	public void remove() throws PersistenceExceptionMessage {
		DAOInstance().remove(this);
	}

	
	public PisCofins merge() throws PersistenceExceptionMessage {
		return DAOInstance().merge(this);
	}

	
	public ArrayList<PisCofins> findAll() throws PersistenceExceptionMessage {
		return (ArrayList<PisCofins>) DAOInstance().findAll();
	}
	public ArrayList<PisCofins> findAllEntrada() throws PersistenceExceptionMessage {
		return (ArrayList<PisCofins>) DAOInstance().findAllByEntrada();
	}
	public ArrayList<PisCofins> findAllSaida() throws PersistenceExceptionMessage {
		return (ArrayList<PisCofins>) DAOInstance().findAllBySaida();
	}
	
	public String toString() {
		return getId() +" - " +getDescricao();
	}
	
	public Object getInstanceFromArray(String[] str) {
		for (String string : str) {
			if (string == null)
				return null;
			if (string.trim().length() == 0)
				return null;
		}
		
		if (str != null && str.length == 5) {
			PisCofins pc = new PisCofins();
			pc.setId(str[0].trim());
			pc.setSentido(str[1].trim().charAt(0));
			pc.setDescricao(str[2].trim());
			pc.setAliquotaEntrada(Double.parseDouble(str[3].trim()));
			pc.setAliquotaSaida(Double.parseDouble(str[4].trim()));
			return pc;
		}
		return null;
	}
	@SuppressWarnings("unchecked")
	
	public Collection<PisCofins>  order(ArrayList entities) {
		Collections.sort(entities);
		return entities;
	}
	
	public int removeAll() throws PersistenceExceptionMessage {
		return DAOInstance().removeAll();
	}
	
	public int compareTo(PisCofins o) {
		return getId().compareTo(o.getId());
	}
		
	public ArrayList<String> getArrayAsString() throws PersistenceExceptionMessage {
		ArrayList<String> strs = new ArrayList<String>();
		for (PisCofins pc : order((ArrayList) DAOInstance().findAll())) {
			strs.add(pc.getId() +"|" +pc.getSentido() +"|" +pc.getDescricao() +"|" 
					+pc.getAliquotaEntrada() +"|" +pc.getAliquotaSaida());
		}
		return strs;
	}
	
}
